<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/21
 * Time: 15:18
 */

namespace app\admin\controller;


use cmf\controller\AdminBaseController;
use think\Db;

class LandsController extends AdminBaseController
{
    protected $arrWhere = [];

    //咨询列表
    public function index_list()
    {
        $phone = trim(request()->param('phone'));
        $nick = trim(request()->param('nick'));
        $status = trim(request()->param('status'));
        $create_time = trim(request()->param('create_time'));
        $end_time = trim(request()->param('end_time'));
        //筛选条件
        if(!empty($phone)){
            $this->arrWhere['phone'] = ['like','%'.$phone.'%'];
        }
        if(!empty($nick)){
            $this->arrWhere['nick'] = ['like','%'.$nick.'%'];
        }
        if(!empty($status)){
            $this->arrWhere['status'] = ['like','%'.$status.'%'];
        }
        if(!empty($create_time)&&!empty($end_time)){
            $this->arrWhere['create_time'] = ['between time',[strtotime($create_time),strtotime($end_time)]];
        }
        //时间筛选
        if(empty($status)){
            $status ='';
        }
        $list = Db::name('lands')->where($this->arrWhere)->order('id desc')->paginate(8);

        $list->appends(['phone'=>$phone,'nick'=>$nick,'status'=>$status,'end_time'=>$end_time,'create_time'=>$create_time]);
        //获取分页参数
        $page = $list->render();

        return view('',['list'=>$list,'page'=>$page,'status'=>$status,'end_time'=>$end_time,'create_time'=>$create_time]);
    }


    /**
     * @return \think\response\View
     * @throws \think\exception\DbException
     * 咨询备注
     * id     咨询信息id
     * remarks   备注
     */
    public function lands_remarks(){

        $data = request()->param();
        $res = Db::name('lands')->where('id',$data['id'])->update(['status'=>2,'remarks'=>$data['remarks']]);
        if($res){
            return_json(1,'添加成功');
        }else{
            return_json(0,'添加失败');
        }
    }

    //赔偿预估
    public function predict()
    {
        $phone = trim(request()->param('phone'));
        //筛选条件
        if(!empty($phone)){
            $this->arrWhere['phone'] = ['like','%'.$phone.'%'];
        }
        $list = Db::name('predict')->where($this->arrWhere)->paginate(8);

        $list->appends(['phone'=>$phone]);
        //获取分页参数
        $page = $list->render();

        return view('',['list'=>$list,'page'=>$page]);
    }

    /**
     * 导出Excel
     */
    public function excel(){
        /**导出excel**/
        header("Content-type:application/vnd.ms-excel");   //声明内容类型为excel
        header("Content-Disposition:attachment;filename=表单.xls");  //content-disposition设置attachment为弹窗下载，inline时会内嵌浏览器显示，当然对jpg等文件有效，excel文件不能内嵌，可自行翻阅文档了解；filename定义文件名称与扩展名
        echo "id\t";
        echo "姓名\t";
        echo "手机号\t";
        echo "来源类型\t";
        echo "时间\t";
        echo "url\t";
        echo "状态\t";
        echo "备注";
        $list = Db::name('lands')->select()->toArray();
        foreach ($list as $k=>$v){
            if($v['status']==1){
                $list[$k]['status'] = '未处理';
            }else{
                $list[$k]['status'] = '已处理';
            }
        }
        foreach ($list as $k=>$v){
            echo "\n";
            echo $v['id']." \t";
            echo $v['nick']." \t";
            echo $v['phone']." \t";
            echo $v['type']." \t";
            echo $v['date']." \t";
            echo $v['url']." \t";
            echo $v['status']." \t";
            echo $v['remarks'];
        }
//
//        for($i=1; $i<=20;$i++){
//            echo "\n";
//            echo date("YmdHis").rand(1000,9999)." \t";
//            echo "第{$i}个用户\t";
//            echo "商品名称 $i\t";
//            echo "支付成功\t";
//            echo "微信支付\t";
//            echo uniqid()."\t";
//            echo date("Y-m-d H:i:s");
//        }
    }
}